package cn.emay.emas.interfacee;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class EmayHttpInterfaceExmple {

	public void vSend(boolean isEncry,String public_key, String private_key, String[] mobiles, String smscontext, int level, Date sendTime,String ip,int port) throws UnsupportedEncodingException {
		Map<String, String> params = new HashMap<String, String>();
		params.put("public_key", public_key);
		SendSmsData data = new SendSmsData(mobiles, smscontext, sendTime, level);
		String smsdata = JsonHelper.toJsonString(data);
		if(isEncry){
			smsdata = EncryptionUtils.parseByte2HexStr(EncryptionUtils.aesEncrypt(smsdata, private_key));
			params.put("encry", "true");
		}else{
			smsdata = URLEncoder.encode(smsdata, "UTF-8");
			params.put("encry", "false");
		}
		String sign = EncryptionUtils.parseByte2HexStr(EncryptionUtils.aesEncrypt(DateUtil.toString(new Date(), "yyyyMMddHHmmssSSS"), private_key));
		params.put("sms_data", smsdata);
		params.put("sign", sign);
		System.out.println("send sms >>>>>> ");
		System.out.println("\tpublic_key : " + public_key);
		System.out.println("\tsms_data : " + smsdata);
		System.out.println("\tsign : " + sign);
		HttpRequestBody request = new HttpRequestBody("http://"+ip+":"+port+"/sendSMS", "UTF-8", null, null, null, params);
		HttpClient client = new HttpClient(180, 180, false);
		long begin = System.currentTimeMillis();
		HttpResponseBody res = client.service(request);
		long end = System.currentTimeMillis();
		System.out.println("send sms result  >>>>>>\n\t" + res.getResultString());
		System.out.println("send sms use time : " + (end - begin) + "ms\n");
	}

	public void vGetReport(String public_key, String private_key,String ip,int port) {
		String sign = EncryptionUtils.parseByte2HexStr(EncryptionUtils.aesEncrypt(DateUtil.toString(new Date(), "yyyyMMddHHmmssSSS"), private_key));
		Map<String, String> params = new HashMap<String, String>();
		params.put("public_key", public_key);
		params.put("sign", sign);
		System.out.println("get Report >>>>>> ");
		System.out.println("\tpublic_key : " + public_key);
		System.out.println("\tsign : " + sign);
		HttpRequestBody request = new HttpRequestBody("http://"+ip+":"+port+"/getReport", "UTF-8", null, null, null, params);
		HttpClient client = new HttpClient(180, 180, false);
		long begin = System.currentTimeMillis();
		HttpResponseBody res = client.service(request);
		long end = System.currentTimeMillis();
		System.out.println("get Report result  >>>>>>\n\t" + res.getResultString());
		System.out.println("get Report use time : " + (end - begin) + "ms\n");
	}

	public void vGetUpStream(String public_key, String private_key,String ip,int port) {
		String sign = EncryptionUtils.parseByte2HexStr(EncryptionUtils.aesEncrypt(DateUtil.toString(new Date(), "yyyyMMddHHmmssSSS"), private_key));
		Map<String, String> params = new HashMap<String, String>();
		params.put("public_key", public_key);
		params.put("sign", sign);
		System.out.println("get Upstream >>>>>> ");
		System.out.println("\tpublic_key : " + public_key);
		System.out.println("\tsign : " + sign);
		HttpRequestBody request = new HttpRequestBody("http://"+ip+":"+port+"/getUpstream", "UTF-8", null, null, null, params);
		HttpClient client = new HttpClient(180, 180, false);
		long begin = System.currentTimeMillis();
		HttpResponseBody res = client.service(request);
		long end = System.currentTimeMillis();
		System.out.println("get Upstream result  >>>>>>\n\t" + res.getResultString());
		System.out.println("get Upstream use time : " + (end - begin) + "ms\n");
	}

	public static class SendSmsData {

		private String[] mobiles;

		private String smscontent;

		private int level = 3;

		private Date sendtime;

		public SendSmsData(String[] mobiles, String smscontent, Date sendtime, int level) {
			this.mobiles = mobiles;
			this.smscontent = smscontent;
			this.level = level;
			this.sendtime = sendtime;
		}

		public String[] getMobiles() {
			return mobiles;
		}

		public void setMobiles(String[] mobiles) {
			this.mobiles = mobiles;
		}

		public String getSmscontent() {
			return smscontent;
		}

		public void setSmscontent(String smscontent) {
			this.smscontent = smscontent;
		}

		public int getLevel() {
			return level;
		}

		public void setLevel(int level) {
			this.level = level;
		}

		public Date getSendtime() {
			return sendtime;
		}

		public void setSendtime(Date sendtime) {
			this.sendtime = sendtime;
		}
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		//ip
		String ip = "100.100.10.140";
		int port = 8089;
		//公钥
		String pubkey = "EMAS-300E-1F76-5B4B";
		//私钥
		String prikey = "E1F765B4BEFC46C4";
		//是否加密
		boolean encry = true;
		//手机号
		String mobiles = "18888888888";
		//内容
		String context = "【清华大学】 i love you ! ";
		//优先级
		int level = 3;
		// 定时时间
//		Date sendtime = DateUtil.parseDate("2016-03-15 12:00:00", "yyyy-MM-dd HH:mm:ss"); // 定时发送
		Date sendtime = null; // 非定时发送
		// 发送
		new EmayHttpInterfaceExmple().vSend(encry,pubkey, prikey, mobiles.split(","), context, level, sendtime,ip,port);
		// 获取状态报告
//		 new EmayHttpInterfaceExmple().vGetReport(pubkey, prikey,ip,port);
		// 获取上行
//		 new EmayHttpInterfaceExmple().vGetUpStream(pubkey, prikey,ip,port);
	}

}
